home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: Pointers to member functions HOW?
- Date: 24 Jan 1996 19:23:18 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan24142318@g7240065.bridge.bst.bls.com>
- References: <31067074.6B53@compuserve.com>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: Roberto Ortiz's message of Wed, 24 Jan 1996 13:46:28 -0400
-
- In article <31067074.6B53@compuserve.com> Roberto Ortiz <74011.3205@compuserve.com> writes:
-
- : I've been attempting for some time now to declare a pointer to a class'
- : memeber function. I have no problem with pointers to regular functions, but
- : with class members, I run into one of two problems:
-
- : a) I can't assign the function to the pointer.
-
- You cannot assign a class member function to a regular function pointer of
- the same signature, unless the class member function is static.
-
- : b) I can't call the pointer as a function.
-
- You require an instance to call the pointer to member function on - using
- the operator '.*' (or '->*' for a pointer to an instance).
-
- : #include <stdio.h>
-
- : void RegularFunction(int iVal)
- : {
- : printf("[%d]\n", iVal);
- : };
-
- : class TTest {
- : public:
- : void MemberFunction(int iVal)
- : {
- : printf("[%d]\n", iVal);
- : };
- : };
-
- : void main()
-
- This should be:
- int main(void)
-
- : {
- : typedef void (TFuncVoidInt)(int);
-
- : TFuncVoidInt *Func0;
- : TFuncVoidInt *Func1;
-
- : void (TTest::*Func2)(int);
-
- : Func0 = RegularFunction;
- : Func1 = TTest::MemberFunction; // Error;
- : Func2 = &TTest::MemberFunction;
- ^ this is unnecessary, but not wrong
-
- : Func0(100);
- : Func1(100);
- : Func2(100); // Error
- try:
- TTest test;
- (test.*)Func2(100);
- : };
-
- : Any suggestions?
-
- Above.
-
- Regards
- -A.
- --
- | A.Champion |
-